﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace KD.Sms.Card
{
    internal class MfReader
    {
        private string m_szGUID;

        private string m_szCardNo;

        public int m_nDevice;

        private string m_szDeviceType;

        private string m_szDeviceName;

        public string m_szStateInfo;

        private bool m_bOpened;

        private bool m_bRunning;

        private string m_szPortName;

        private int m_nBaudRate;

        private short m_nCommPort;

        private Thread m_hThread;

        public string DeviceType
        {
            get
            {
                return this.m_szDeviceType;
            }
            set
            {
                this.m_szDeviceType = value;
            }
        }

        public string DeviceName
        {
            get
            {
                return this.m_szDeviceName;
            }
            set
            {
                this.m_szDeviceName = value;
            }
        }

        public string StateInfo
        {
            get
            {
                return this.m_szStateInfo;
            }
            set
            {
                this.m_szStateInfo = value;
            }
        }

        public string PortName
        {
            get
            {
                return this.m_szPortName;
            }
            set
            {
                this.m_szPortName = value;
            }
        }

        public int BaudRate
        {
            get
            {
                return this.m_nBaudRate;
            }
            set
            {
                this.m_nBaudRate = value;
            }
        }

        public short CommPort
        {
            get
            {
                return this.m_nCommPort;
            }
            set
            {
                this.m_nCommPort = value;
            }
        }

        public string GUID
        {
            get
            {
                return this.m_szGUID;
            }
            set
            {
                this.m_szGUID = value;
            }
        }

        public string CardNo
        {
            get
            {
                return this.m_szCardNo;
            }
            set
            {
                this.m_szCardNo = value;
            }
        }

        public bool IsOpen
        {
            get
            {
                return this.m_bOpened;
            }
        }

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int rf_init(short port, int baud);

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern short rf_anticoll(int icdev, int bcnt, out uint snr);

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern short rf_reset(int icdev, int msec);

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern short rf_request(int icdev, int mode, out ushort tagtype);

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern short rf_beep(int icdev, int msec);

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern short a_hex([MarshalAs(UnmanagedType.LPArray)] byte[] asc, [MarshalAs(UnmanagedType.LPArray)] byte[] hex, int len);

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern short rf_load_key(int icdev, int mode, int secnr, [MarshalAs(UnmanagedType.LPArray)] byte[] keybuff);

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern short hex_a([MarshalAs(UnmanagedType.LPArray)] byte[] hex, [MarshalAs(UnmanagedType.LPArray)] byte[] asc, int len);

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern short rf_authentication(int icdev, int mode, int secnr);

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern short rf_read(int icdev, int blocknr, [MarshalAs(UnmanagedType.LPArray)] byte[] databuff);

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern short rf_select(int icdev, uint snr, out byte size);

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern short rf_write(int icdev, int blocknr, [MarshalAs(UnmanagedType.LPArray)] byte[] databuff);

        [DllImport("mwrf32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern short rf_exit(int icdev);

        public MfReader()
        {

            this.m_hThread = null;
            //
            this.m_szGUID = "";
            this.m_szCardNo = "";
            this.m_nDevice = 0;
            this.m_szDeviceType = "";
            this.m_szDeviceName = "";
            this.m_szStateInfo = "";
            this.m_bOpened = false;
            this.m_bRunning = false;
            this.m_szPortName = "";
            this.m_nBaudRate = 0;
            this.m_nCommPort = 0;
            this.m_hThread = null;
        }

        public MfReader(short nPort, int nBaudRate)
        {
            //
            this.m_hThread = null;
            //
            this.m_szPortName = "COM" + nPort.ToString();
            this.m_nBaudRate = nBaudRate;
            this.m_nCommPort = nPort;
        }

        public MfReader(string strParams)
        {
            //
            this.m_hThread = null;
            //
            string[] array = strParams.Split(new char[]
            {
                ','
            });
            if (array.Length >= 2)
            {
                this.m_nCommPort = Convert.ToInt16(array[0]);
                this.m_szPortName = "COM" + this.m_nCommPort.ToString();
                this.m_nBaudRate = Convert.ToInt32(array[1]);
            }
        }

        public bool Open()
        {
            bool result;
            try
            {
                if (!this.m_bOpened)
                {
                    this.m_nDevice = MfReader.rf_init(this.m_nCommPort, this.m_nBaudRate);
                    if (this.m_nDevice > 0)
                    {
                        this.m_szStateInfo = "open";
                        this.m_bOpened = true;
                        result = true;
                    }
                    else
                    {
                        this.m_szStateInfo = "close";
                        this.m_bOpened = false;
                        result = false;
                    }
                }
                else
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                this.m_szStateInfo = "close";
                this.m_bOpened = false;
                result = false;
            }
            return result;
        }

        public bool Close()
        {
            bool result;
            if (this.m_nDevice > 0)
            {
                MfReader.rf_exit(this.m_nDevice);
                this.m_nDevice = 0;
                this.m_szStateInfo = "close";
                this.m_bOpened = false;
                result = true;
            }
            else
            {
                result = true;
            }
            return result;
        }

        public void StartUse()
        {
            if (this.m_bOpened)
            {
                this.m_bRunning = true;
                this.m_hThread = new Thread(new ThreadStart(this.DeviceThread));
                this.m_hThread.Start();
            }
        }

        private void DeviceThread()
        {
            try
            {
                ushort num = 0;
                byte b = 0;
                uint snr = 0u;
                int num2 = 3;
                string s = "ffffffffffff";
                byte[] asc = new byte[17];
                byte[] array = new byte[7];
                byte[] array2 = new byte[16];
                byte[] array3 = new byte[32];
                for (int i = 0; i < 16; i++)
                {
                    array2[i] = 0;
                }
                for (int i = 0; i < 32; i++)
                {
                    array3[i] = 0;
                }
                while (this.m_bRunning)
                {
                    if (!this.m_bOpened)
                    {
                        Thread.Sleep(300);
                    }
                    else
                    {
                        if (!this.m_bRunning)
                        {
                            break;
                        }
                        Thread.Sleep(50);
                        MfReader.rf_reset(this.m_nDevice, 3);
                        if (!this.m_bRunning)
                        {
                            break;
                        }
                        Thread.Sleep(50);
                        int num3 = (int)MfReader.rf_request(this.m_nDevice, 1, out num);
                        if (num3 == 0)
                        {
                            if (!this.m_bRunning)
                            {
                                break;
                            }
                            Thread.Sleep(50);
                            num3 = (int)MfReader.rf_anticoll(this.m_nDevice, 0, out snr);
                            if (num3 == 0)
                            {
                                string szGUID = snr.ToString("X");
                                this.m_szGUID = szGUID;
                                if (!this.m_bRunning)
                                {
                                    break;
                                }
                                Thread.Sleep(50);
                                num3 = (int)MfReader.rf_select(this.m_nDevice, snr, out b);
                                if (num3 == 0)
                                {
                                    asc = Encoding.ASCII.GetBytes(s);
                                    MfReader.a_hex(asc, array, 12);
                                    if (!this.m_bRunning)
                                    {
                                        break;
                                    }
                                    Thread.Sleep(50);
                                    num3 = (int)MfReader.rf_load_key(this.m_nDevice, 0, num2, array);
                                    if (num3 == 0)
                                    {
                                        if (!this.m_bRunning)
                                        {
                                            break;
                                        }
                                        Thread.Sleep(50);
                                        num3 = (int)MfReader.rf_authentication(this.m_nDevice, 0, num2);
                                        if (num3 == 0)
                                        {
                                            if (!this.m_bRunning)
                                            {
                                                break;
                                            }
                                            Thread.Sleep(50);
                                            num3 = (int)MfReader.rf_read(this.m_nDevice, num2 * 4 + 1, array2);
                                            if (num3 == 0)
                                            {
                                                MfReader.hex_a(array2, array3, 16);
                                                string @string = Encoding.ASCII.GetString(array3);
                                                this.m_szCardNo = @string.Substring(22, 10);
                                                if (!this.m_bRunning)
                                                {
                                                    break;
                                                }
                                                Thread.Sleep(50);
                                                num3 = (int)MfReader.rf_beep(this.m_nDevice, 10);
                                                if (!this.m_bRunning)
                                                {
                                                    break;
                                                }
                                                Thread.Sleep(50);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                this.m_bOpened = false;
            }
            catch (Exception ex)
            {
                //this.WriteLog(this.m_szDeviceName + ")错误：" + ex.Message);
            }
        }

        public string GetCardNo()
        {
            ushort num = 0;
            uint num2 = 0u;
            Thread.Sleep(50);
            MfReader.rf_reset(this.m_nDevice, 3);
            Thread.Sleep(50);
            int num3 = (int)MfReader.rf_request(this.m_nDevice, 1, out num);
            string result;
            if (num3 != 0)
            {
                result = "";
            }
            else
            {
                Thread.Sleep(50);
                num3 = (int)MfReader.rf_anticoll(this.m_nDevice, 0, out num2);
                if (num3 != 0)
                {
                    result = "";
                }
                else
                {
                    string szGUID = num2.ToString("X");
                    this.m_szGUID = szGUID;
                    result = this.m_szGUID;
                }
            }
            return result;
        }

        public bool ReadData()
        {
            bool result;
            if (!this.m_bOpened)
            {
                result = false;
            }
            else
            {
                try
                {
                    ushort num = 0;
                    byte b = 0;
                    uint snr = 0u;
                    int num2 = 3;
                    string s = "ffffffffffff";
                    byte[] asc = new byte[17];
                    byte[] array = new byte[7];
                    byte[] array2 = new byte[16];
                    byte[] array3 = new byte[32];
                    for (int i = 0; i < 16; i++)
                    {
                        array2[i] = 0;
                    }
                    for (int i = 0; i < 32; i++)
                    {
                        array3[i] = 0;
                    }
                    MfReader.rf_reset(this.m_nDevice, 3);
                    int num3 = (int)MfReader.rf_request(this.m_nDevice, 1, out num);
                    if (num3 != 0)
                    {
                        result = false;
                    }
                    else
                    {
                        num3 = (int)MfReader.rf_anticoll(this.m_nDevice, 0, out snr);
                        if (num3 != 0)
                        {
                            result = false;
                        }
                        else
                        {
                            string szGUID = snr.ToString("X");
                            this.m_szGUID = szGUID;
                            num3 = (int)MfReader.rf_select(this.m_nDevice, snr, out b);
                            if (num3 != 0)
                            {
                                result = false;
                            }
                            else
                            {
                                asc = Encoding.ASCII.GetBytes(s);
                                MfReader.a_hex(asc, array, 12);
                                num3 = (int)MfReader.rf_load_key(this.m_nDevice, 0, num2, array);
                                if (num3 != 0)
                                {
                                    result = false;
                                }
                                else
                                {
                                    num3 = (int)MfReader.rf_authentication(this.m_nDevice, 0, num2);
                                    if (num3 != 0)
                                    {
                                        result = false;
                                    }
                                    else
                                    {
                                        num3 = (int)MfReader.rf_read(this.m_nDevice, num2 * 4 + 1, array2);
                                        if (num3 != 0)
                                        {
                                            result = false;
                                        }
                                        else
                                        {
                                            MfReader.hex_a(array2, array3, 16);
                                            string @string = Encoding.ASCII.GetString(array3);
                                            this.m_szCardNo = @string.Substring(22, 10);
                                            num3 = (int)MfReader.rf_beep(this.m_nDevice, 10);
                                            result = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //this.WriteLog(this.m_szDeviceName + ")错误：" + ex.Message);
                    result = false;
                }
            }
            return result;
        }

        public bool WriteData(string strData)
        {
            bool result;
            if (!this.m_bOpened)
            {
                result = false;
            }
            else if (strData.Length > 32)
            {
                result = false;
            }
            else
            {
                int length = strData.Trim().Length;
                for (int i = 0; i < 32 - length; i++)
                {
                    strData = "0" + strData;
                }
                ushort num = 0;
                byte b = 0;
                uint snr = 0u;
                int num2 = 3;
                string s = "ffffffffffff";
                byte[] asc = new byte[17];
                byte[] array = new byte[7];
                byte[] array2 = new byte[16];
                byte[] asc2 = new byte[32];
                MfReader.rf_reset(this.m_nDevice, 3);
                int num3 = (int)MfReader.rf_request(this.m_nDevice, 1, out num);
                if (num3 != 0)
                {
                    result = false;
                }
                else
                {
                    num3 = (int)MfReader.rf_anticoll(this.m_nDevice, 0, out snr);
                    if (num3 != 0)
                    {
                        result = false;
                    }
                    else
                    {
                        snr.ToString("X");
                        num3 = (int)MfReader.rf_select(this.m_nDevice, snr, out b);
                        if (num3 != 0)
                        {
                            result = false;
                        }
                        else
                        {
                            asc = Encoding.ASCII.GetBytes(s);
                            MfReader.a_hex(asc, array, 12);
                            num3 = (int)MfReader.rf_load_key(this.m_nDevice, 0, num2, array);
                            if (num3 != 0)
                            {
                                result = false;
                            }
                            else
                            {
                                num3 = (int)MfReader.rf_authentication(this.m_nDevice, 0, num2);
                                if (num3 != 0)
                                {
                                    result = false;
                                }
                                else
                                {
                                    asc2 = Encoding.ASCII.GetBytes(strData);
                                    MfReader.a_hex(asc2, array2, 32);
                                    num3 = (int)MfReader.rf_write(this.m_nDevice, num2 * 4 + 1, array2);
                                    result = (num3 == 0);
                                }
                            }
                        }
                    }
                }
            }
            return result;
        }

        public void StopUse()
        {
            this.m_bRunning = false;
            int num = 0;
            while (num < 50 && this.m_bOpened)
            {
                Thread.Sleep(100);
                num++;
            }
            this.Close();
        }


        static MfReader()
        {
            
        }
    }
}
